package com.shtel.bss.saas.tenant.controller;

import com.github.pagehelper.PageInfo;
import com.shtel.bss.saas.core.data.common.controller.DataController;
import com.shtel.bss.saas.tenant.dto.SysTenantDTO;
import com.shtel.bss.saas.tenant.mapstrut.EntityDtoMapper;
import com.shtel.bss.saas.tenant.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 租户表 controller
 *
 * @author xiezhiwei 2020-02-05 22:03
 */
@RestController
@RequestMapping("/bam/tenants")
public class SysTenantController extends DataController {
    @Autowired
    private ISysTenantService sysTenantService;


    @GetMapping()
    public PageInfo<SysTenantDTO> querySysTenantList(@RequestParam(required = false) String tenantName,
                                                     @RequestParam(required = false) String email,
                                                     @RequestParam(required = false, defaultValue = "10") int pageSize,
                                                     @RequestParam(required = false, defaultValue = "1") int pageNum) {
        return EntityDtoMapper.INSTANCE.
                toSysTenantDTOPageInfo(sysTenantService.querySysTenantList(tenantName, email, pageSize, pageNum));
    }

    @GetMapping("{tenantId}")
    public SysTenantDTO querySysTenantByTenantId(@PathVariable Long tenantId) {
        return EntityDtoMapper.INSTANCE.toSysTenantDTO(sysTenantService.querySysTenantByTenantId(tenantId));
    }

    @PostMapping()
    public void saveSysTenant(@RequestBody SysTenantDTO sysTenant) {
        sysTenantService.saveSysTenant(EntityDtoMapper.INSTANCE.toSysTenantBo(sysTenant));
    }

    @PutMapping("/{tenantId}")
    public void updateSysTenant(@PathVariable("tenantId") Long tenantId, @RequestBody SysTenantDTO sysTenant) {
        sysTenant.setTenantId(tenantId);
        sysTenantService.updateSysTenant(EntityDtoMapper.INSTANCE.toSysTenantBo(sysTenant));
    }

    @DeleteMapping("/{tenantId}")
    public void deleteSysTenant(@PathVariable("tenantId") Long[] tenantIds) {
        sysTenantService.removeSysTenant(Arrays.asList(tenantIds));
    }

    @PatchMapping("/{tenantId}")
    public void prohibitSysTenant(@PathVariable Long tenantId) {
        sysTenantService.prohibitSysTenant(tenantId);
    }
}